﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WiimoteLib;

namespace TayoukiNameSpace
{
    public partial class ClockGame : TayoukiNameSpace.GameForm
    {
        private List<Image> clockImages;

        private Image wiiNormal;

        private Image wiiLeft;

        private Image wiiRight;

        private bool btnPressed;

        //contains the accelerator x,y values for the clock : clockwise direction from 12 to 11
        private double[,] values = {{0,-1},{0.5,-0.866},{0.866,-0.5},
                                  {1,0},{0.866,0.5},{0.5,0.866},
                                  {0,1},{-0.5,0.866},{-0.866,0.5},
                                  {-1,0},{-0.866,-0.5},{-0.5,-0.866}};

        static int dials = 12;
        int counter = 0;
        int current = 0;
        int done = 0;
        int end = 5;
        int difference;

        public ClockGame()
        {
            InitializeComponent();
            clockImages = new List<Image>();
            btnPressed = false;
            initializeImages();
            end = RandomNumber(1, 11);
            difference = Math.Min(Math.Abs(end - current), Math.Abs(end-12));

            //add Event Handler to the form
            this.KeyPreview = true;
            this.KeyPress += new KeyPressEventHandler(ClockGame_KeyPress);
        }
        private int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        private void ClockGame_Load(object sender, EventArgs e)
        {
            this.textInstructions.Text = "Move the handle to "+end;
            this.workspacePictureBox.Image = clockImages[0];
            this.wiiInstructionsPictureBox.Image = wiiNormal;
            changeAvatarImg(GameForm.GOOD_JOB);
        }

        private void initializeImages()
        {
            //Clock Images
            clockImages.Add(Properties.Resources.clock_12);
            clockImages.Add(Properties.Resources.clock_1);
            clockImages.Add(Properties.Resources.clock_2);
            clockImages.Add(Properties.Resources.clock_3);
            clockImages.Add(Properties.Resources.clock_4);
            clockImages.Add(Properties.Resources.clock_5);
            clockImages.Add(Properties.Resources.clock_6);
            clockImages.Add(Properties.Resources.clock_7);
            clockImages.Add(Properties.Resources.clock_8);
            clockImages.Add(Properties.Resources.clock_9);
            clockImages.Add(Properties.Resources.clock_10);
            clockImages.Add(Properties.Resources.clock_11);

            //Wii Instructions
            wiiNormal = Properties.Resources.wii_normal;
            wiiLeft = Properties.Resources.wii_move_left;
            wiiRight = Properties.Resources.wii_move_right;

        }

        //Listens to keypresses to move the clock accordingly 
        //('k' moves left, 'l' moves right)
        void ClockGame_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 1 && e.KeyChar <= 255 && done == 0)
            {
                //l= 108 (right)
                //k= 107 (left)                

                switch (e.KeyChar)
                {
                    //right key detected
                    case (char)108:
                        e.Handled = true;
                        //update counter
                        counter++;
                        if (counter == dials)
                        {
                            counter = 0;
                        }

                        //reached the end
                        if (counter == end)
                        {
                            this.workspacePictureBox.Image = clockImages[counter];
                            this.workspacePictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display happy smiley
                            changeAvatarImg(GameForm.FINISH);
                            
                            //display happy smiley
                            this.wiiInstructionsPictureBox.Image = Properties.Resources.avatar_congrats;
                            this.wiiInstructionsPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update text
                            this.textInstructions.Text = "Congratulations! ";// +e.KeyChar.ToString();

                            //to make sure future presses are not considered
                            done = 1;

                            break;
                        }

                        //this.textInstructions.Text = "Detected " + e.KeyChar.ToString();
                        this.workspacePictureBox.Image = clockImages[counter];
                        this.workspacePictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                        //figure out what image to display in the other two picture boxes
                        int new_diff = 0;
                        if (counter <= 6)
                        {
                            new_diff = Math.Min(Math.Abs(counter - end), (Math.Abs(12 - end) + Math.Abs(0 - counter)));
                        }
                        else
                        {
                            new_diff = Math.Min(Math.Abs(counter - end), (Math.Abs(0 - end) + Math.Abs(12 - counter)));
                        }

                        if (new_diff <= difference)
                        {
                            //display happy smiley
                            changeAvatarImg(GameForm.GOOD_JOB);
                            this.avatarPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display normal wii
                            this.wiiInstructionsPictureBox.Image = wiiNormal;
                            this.wiiInstructionsPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update textbox
                            //this.textInstructions.Text = "Correct: Old diff= " + difference + " New diff= " + new_diff;
                            this.textInstructions.Text = "Correct direction";
                        }
                        else //should be moving left
                        {
                            //display sad smiley
                            changeAvatarImg(GameForm.WRONG);
                            this.avatarPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display move_left wii
                            this.wiiInstructionsPictureBox.Image = wiiLeft;
                            this.wiiInstructionsPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update textbox
                            //this.textInstructions.Text = "Move left: Old diff= " + difference + " New diff= " + new_diff;
                            this.textInstructions.Text = "Wrong direction!";
                        }

                        //update difference
                        difference = new_diff;

                        break;

                    //left key detected
                    case (char)107:
                        e.Handled = true;
                        //update counter
                        counter--;
                        if (counter < 0)
                        {
                            counter = 11;
                        }

                        //reached the end
                        if (counter == end)
                        {
                            this.workspacePictureBox.Image = clockImages[counter];
                            this.workspacePictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //display happy smiley
                            changeAvatarImg(GameForm.FINISH);

                            //display happy smiley
                            this.wiiInstructionsPictureBox.Image = Properties.Resources.avatar_congrats;
                            this.wiiInstructionsPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update text
                            this.textInstructions.Clear();
                            this.textInstructions.Text = "Congratulations! ";// +e.KeyChar.ToString();

                            //to make sure future presses are not considered
                            done = 1;

                            break;
                        }

                        //this.textInstructions.Text = "Detected " + e.KeyChar.ToString();
                        this.workspacePictureBox.Image = clockImages[counter];
                        this.workspacePictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                        //figure out what image to display in the other two picture boxes
                        new_diff = 0;
                        if (counter <= 6)
                        {
                            new_diff = Math.Min(Math.Abs(counter - end), (Math.Abs(12 - end) + Math.Abs(0 - counter)));
                        }
                        else
                        {
                            new_diff = Math.Min(Math.Abs(counter - end), (Math.Abs(0 - end) + Math.Abs(12 - counter)));
                        }

                        if (new_diff <= difference)
                        {
                            //display happy smiley
                            changeAvatarImg(GameForm.GOOD_JOB);

                            //display normal wii
                            this.wiiInstructionsPictureBox.Image = wiiNormal;
                            this.wiiInstructionsPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update textbox
                            //this.textInstructions.Text = "Correct: Old diff= " + difference + " New diff= " + new_diff;
                            this.textInstructions.Text = "Correct direction";
                        }
                        else //should be moving right
                        {
                            //display sad smiley
                            changeAvatarImg(GameForm.WRONG);
                            

                            //display move_left wii
                            this.wiiInstructionsPictureBox.Image = wiiRight;
                            this.wiiInstructionsPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;

                            //update textbox
                            //this.textInstructions.Text = "Move right: Old diff= " + difference + " New diff= " + new_diff;
                            this.textInstructions.Text = "Wrong Direction!";
                        }

                        //update difference
                        difference = new_diff;

                        break;
                }
            }
        }

        public int clock_arm(float x, float y)
        {
            //see which range these values fall under and figure out the closest arm value

            //special cases for 12,3,6,9
            if ((x >= values[0, 0]- 0.1) && (x<= values[0,0] + 0.1) &&
                (y >= values[0, 1]- 0.1) && (y<= values[0,1] + 0.1)) //12
            {
                return 0;
            }            
            else if ((x >= values[3, 0] - 0.1) && (x <= values[3, 0] + 0.1) &&
                     (y >= values[3, 1] - 0.1) && (y <= values[3, 1] + 0.1)) //3
            {
                return 3;
            }            
            else if ((x >= values[6, 0] - 0.1) && (x <= values[6, 0] + 0.1) &&
                     (y >= values[6, 1] - 0.1) && (y <= values[6, 1] + 0.1)) //6
            {
                return 6;
            }
            else if ((x >= values[9, 0] - 0.1) && (x <= values[9, 0] + 0.1) &&
                     (y >= values[9, 1] - 0.1) && (y <= values[9, 1] + 0.1)) //9
            {
                return 9;
            }

            //remaining cases
            if (x >= 0) //between 12 and 6
            {
                if (y >= 0) //4 or 5
                {
                    if ((x >= values[4, 0] - 0.1) && (x <= values[4, 0] + 0.1) &&
                        (y >= values[4, 1] - 0.1) && (y <= values[4, 1] + 0.1)) //4
                    {
                        return 4;
                    }
                    else if ((x >= values[5, 0] - 0.1) && (x <= values[5, 0] + 0.1) &&
                             (y >= values[5, 1] - 0.1) && (y <= values[5, 1] + 0.1)) //5
                    {
                        return 5;
                    }
                }
                else //1 or 2
                {
                    if ((x >= values[1, 0]- 0.1) && (x<= values[1,0] + 0.1) &&
                        (y >= values[1, 1]- 0.1) && (y<= values[1,1] + 0.1)) //1
                    {
                        return 1;
                    }
                    else if ((x >= values[2, 0] - 0.1) && (x <= values[2, 0] + 0.1) &&
                             (y >= values[2, 1] - 0.1) && (y <= values[2, 1] + 0.1)) //5
                    {
                        return 2;
                    }
                }
            }
            else //between 6 and 12
            {
                if (y >= 0) //7 or 8
                {
                    if ((x >= values[7, 0] - 0.1) && (x <= values[7, 0] + 0.1) &&
                        (y >= values[7, 1] - 0.1) && (y <= values[7, 1] + 0.1)) //7
                    {
                        return 7;
                    }
                    else if ((x >= values[8, 0] - 0.1) && (x <= values[8, 0] + 0.1) &&
                             (y >= values[8, 1] - 0.1) && (y <= values[8, 1] + 0.1)) //8
                    {
                        return 8;
                    }
                }
                else //10 or 11
                {
                    if ((x >= values[10, 0] - 0.1) && (x <= values[10, 0] + 0.1) &&
                        (y >= values[10, 1] - 0.1) && (y <= values[10, 1] + 0.1)) //10
                    {
                        return 10;
                    }
                    else if ((x >= values[11, 0] - 0.1) && (x <= values[11, 0] + 0.1) &&
                             (y >= values[11, 1] - 0.1) && (y <= values[11, 1] + 0.1)) //11
                    {
                        return 11;
                    }
                }
            }
            return 99;
            
        }

        override public void processWiiMoteState(WiimoteState wms)
        {
            //only go ahead if the game is not finished
            if (done == 0)
            {
                float x = wms.AccelState.Values.X;
                float y = wms.AccelState.Values.Y;

                //based on the values figure out the new counter value
                float clock_arm_value = clock_arm(x, y);


                //based on the new counter value figure out the direction of the movement
                //if value didn't change or we are in a range that doesnt relate to any arm
                //we just ignore
                //int left = 0;
                //int right = 0;
                if (counter != clock_arm_value && clock_arm_value != 99)
                {

                    System.Console.WriteLine("Clock arm: " + clock_arm_value);
                    System.Console.WriteLine("Counter: " + counter);
                    //figure out the direction if it were a key press
                    if (clock_arm_value != 0)
                    {
                        //special case
                        if (counter == 0)
                        {
                            if (clock_arm_value == 11) //moved left
                            {
                                System.Console.WriteLine("Moved LEFTC");
                                SendKeys.SendWait("k");
                            }
                            else //moved right
                            {
                                System.Console.WriteLine("Moved RIGHTC");
                                SendKeys.SendWait("l");
                            }
                        }
                        else
                        {
                            if (counter < clock_arm_value) //it means we moved right
                            {
                                //right = 1;
                                System.Console.WriteLine("Moved RIGHT");
                                SendKeys.SendWait("l");
                            }
                            else //we are going left
                            {
                                //left = 1;
                                System.Console.WriteLine("Moved LEFT");
                                SendKeys.SendWait("k");
                            }
                        }
                    }
                    else
                    {
                        if (counter == 11) //moved right
                        {
                            //right = 1;
                            System.Console.WriteLine("Moved RIGHTA");
                            SendKeys.SendWait("l");
                        }
                        else //moved left
                        {
                            //left = 1;
                            System.Console.WriteLine("Moved LEFTA");
                            SendKeys.SendWait("k");
                        }
                    }
                }
            }
            
            
            if (wms.ButtonState.Left)
            {
                if (!btnPressed)
                {
                    System.Console.WriteLine("Pressed left");
                    SendKeys.SendWait("k");
                }
                btnPressed = true;
            }

            else if (wms.ButtonState.Right)
            {
                if (!btnPressed)
                {
                    SendKeys.SendWait("l");
                }
                btnPressed = true;
            }
            else if (wms.ButtonState.Home)
            {
                gameClose();
            }

            else
            {
                btnPressed = false;
            }
        }
    }

    
}
